class Shared::ProvincesController < ApplicationController
  # GET /shared/provinces
  # GET /shared/provinces.xml
  def index
    @title_view = 'Provincias'
    @shared_provinces = Shared::Province.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @shared_provinces }
      format.json  { render :json => @shared_provinces }
    end
  end

  # GET /shared/provinces/1
  # GET /shared/provinces/1.xml
  def show
    @shared_province = Shared::Province.find(params[:id])
    @title_view = 'Provincias'
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @shared_province }
       format.json  { render :json => @shared_province }
    end
  end

  # GET /shared/provinces/new
  # GET /shared/provinces/new.xml
  def new
    if (user_signed_in? && ([2].include?(current_user.role)))
    @shared_province = Shared::Province.new
    @title_view = 'Provincias'
    @departments = Shared::Department.all
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @shared_province }
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /shared/provinces/1/edit
  def edit
    if (user_signed_in? && ([2].include?(current_user.role)))
    @shared_province = Shared::Province.find(params[:id])
    @title_view = 'Provincias'
    @departments = Shared::Department.all
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # POST /shared/provinces
  # POST /shared/provinces.xml
  def create
    if (user_signed_in? && ([2].include?(current_user.role)))
    @shared_province = Shared::Province.new(params[:shared_province])
    @title_view = 'Provincias'
    @departments = Shared::Department.all
    respond_to do |format|
      if @shared_province.save
        format.html { redirect_to(@shared_province, :notice => 'Se ha creado correctamente la provincia.') }
        format.xml  { render :xml => @shared_province, :status => :created, :location => @shared_province }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @shared_province.errors, :status => :unprocessable_entity }
      end
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # PUT /shared/provinces/1
  # PUT /shared/provinces/1.xml
  def update
    if (user_signed_in? && ([2].include?(current_user.role)))
    @shared_province = Shared::Province.find(params[:id])
    @title_view = 'Provincias'
    @departments = Shared::Department.all
    respond_to do |format|
      if @shared_province.update_attributes(params[:shared_province])
        format.html { redirect_to(@shared_province, :notice => 'Se ha actualizado correctamente la provincia.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @shared_province.errors, :status => :unprocessable_entity }
      end
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # DELETE /shared/provinces/1
  # DELETE /shared/provinces/1.xml
  def destroy
    if (user_signed_in? && ([2].include?(current_user.role)))
    @shared_province = Shared::Province.find(params[:id])
    @shared_province.deleted = 1
    @shared_province.save

    respond_to do |format|
      format.html { redirect_to(shared_provinces_url) }
      format.xml  { head :ok }
    end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end
end
